/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPanelOperation.java
 *
 * Created on Dec 4, 2011, 12:49:34 PM
 */
package recitation;

import java.text.SimpleDateFormat;
import java.util.Date;
import target.*;
import xml.*;

/**
 *
 * @author buer
 */
public class JPanelOperation extends javax.swing.JPanel {

    /** Creates new form JPanelOperation */
    public JPanelOperation(Word word, WordSpy wordSpy) {
        initComponents();
        this.word = word;
        this.wordSpy = wordSpy;
        dispStatus(word.status);
        // 新建的面板，文本框内容是不可编辑的
        setEditButtonLabel(false);
        saveWord = new WordWriting();
    }
    
    /**
     * 根据单词记录的状态，显示RadioButton组
     * @param word 
     */
    private void dispStatus(Word.STATUS status) {
        if(status == Word.STATUS.DUMP) {
            jRadioButton3.setSelected(true);
        }
        else if(status == Word.STATUS.REM){
            jRadioButton1.setSelected(true);
        }
        else {
            jRadioButton2.setSelected(true);
        }
    }
    
    public void updateContent(Word word) {
        this.word = word;
        dispStatus(this.word.status);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ChooseWordStatus = new javax.swing.ButtonGroup();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        getOneNewWordButton = new javax.swing.JButton();
        editWordContentButton = new javax.swing.JButton();

        setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        setPreferredSize(new java.awt.Dimension(200, 200));

        ChooseWordStatus.add(jRadioButton1);
        jRadioButton1.setText("记得住！");
        jRadioButton1.setToolTipText("");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        ChooseWordStatus.add(jRadioButton2);
        jRadioButton2.setText("记不住！");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        ChooseWordStatus.add(jRadioButton3);
        jRadioButton3.setText("扔之！");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        getOneNewWordButton.setText("下一个");
        getOneNewWordButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        getOneNewWordButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                getOneNewWordButtonMouseClicked(evt);
            }
        });

        editWordContentButton.setText("编辑");
        editWordContentButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                editWordContentButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(64, 64, 64)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton3)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jRadioButton1)
                        .addComponent(jRadioButton2, javax.swing.GroupLayout.Alignment.LEADING))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(editWordContentButton, 0, 0, Short.MAX_VALUE)
                        .addComponent(getOneNewWordButton, javax.swing.GroupLayout.Alignment.LEADING)))
                .addGap(61, 61, 61))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton3)
                .addGap(18, 18, 18)
                .addComponent(getOneNewWordButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(editWordContentButton)
                .addContainerGap(38, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

	private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
	// TODO add your handling code here:    
		//单词状态改为“记得住”
		word.status = Word.STATUS.REM;                              //
	}//GEN-LAST:event_jRadioButton1ActionPerformed

	private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
	// TODO add your handling code here:
		//单词状态改为“记不住”
		word.status = Word.STATUS.NREM;
	}//GEN-LAST:event_jRadioButton2ActionPerformed

	private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
	// TODO add your handling code here:    
		//单词状态改为“不记了！”
		word.status = Word.STATUS.DUMP;
	}//GEN-LAST:event_jRadioButton3ActionPerformed

    /**
     * 根据不同的编辑状态设置“编辑”按钮的文字标签。
     * 若点击完成，同时保存用户修改的单词信息。
     * @param flag 
     */
    private void setEditButtonLabel(boolean flag) {
        if (flag) {
            editWordContentButton.setText("完成");
        } else {
            editWordContentButton.setText("编辑");
            word.content = wordSpy.getContent();
            word.tricks = wordSpy.getTricks();
        }
    }
    private void getOneNewWordButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_getOneNewWordButtonMouseClicked
        // TODO add your handling code here:
        //保存单词信息
        //if (Recitation.editable) {
            //this.word.content=JPanelWordName.getShowText();
            Date date = new Date();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");//转换格式
            word.date = sdf.format(date);
            
            saveWord.wordWriting(word);
        //}
        //返回下一个单词
        Recitation.nextRecitation();

    }//GEN-LAST:event_getOneNewWordButtonMouseClicked

    private void editWordContentButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_editWordContentButtonMouseClicked
        // 改变可编辑性
        wordSpy.toggleEditable();
        // 设置可编辑性
        wordSpy.setEditable();
        setEditButtonLabel(wordSpy.isEditable());
    }//GEN-LAST:event_editWordContentButtonMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup ChooseWordStatus;
    private javax.swing.JButton editWordContentButton;
    private javax.swing.JButton getOneNewWordButton;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    // End of variables declaration//GEN-END:variables
    private Word word;
    private WordSpy wordSpy;
    private WordWriting saveWord;
}
